<?php
/**
 *  NevuxBB - Free Forum Software
 *  Copyright (C) 2008, 2010 NevuxBB (No one specific author)*  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Update System used in ACP
 * File: update_class.php
 * Purpose: Used for updating the system
 * Todo: Test it
 *
 */

class UpdateSystem {
	private $current_version;
	private $default_update_server;
	private $update_to_version;
	
	/**
	* Initialisation function
	*
	* @return void
	* @access void
	*/	
	function __construct() {
		$query = "SELECT * FROM `nbb_settings_update` WHERE `upd_default` = 1 LIMIT 1";
		
		// fetch this as an array
		$this->default_update_server = Db_Abstraction::fetch_array($query);
	}
	
	/**
	* Define current version
	*
	* @param numeric $number
	* @return boolean
	* @access public
	*/	
	public function curr_version($number) {
		// working off of build numbers
		if(is_numeric($number)) {
			// valid
			$this->current_version = $number;
			return true;
		} else {
			// invalid
			return false;
		}
	}
	
	/**
	* Sets the number we're wanting to update to
	*
	* @param string $number
	* @access public
	*/
	public function updateToData($number) {
		// update with the new number
		$this->update_to_version = $number;
	}
	
	/**
	* Execute requests
	*
	* @param string $do
	* @return boolean
	* @access public
	*/	
	public function execute_request($do) {
		// which task to execute?
		switch($do) {
			case 'curr':
				// find out information about this version
				$curr_info_url = $this->default_update_server['upd_address'] . 'r.php?do=curr&platform=xml&ver=' . $this->current_version;
				
				// make a data call with this url
				$call_results = KernelBaseSystem::fetchWebFile($curr_info_url);
				
				// valid result?
				if($call_results === false || $call_results === NULL) {
					// update server down?
					return false;
				} else {
					// return update servers's results
					return $call_results;
				}
				
				// end
				break;
			case 'future_version':
				// get the next version from our current version
				$get_ver_data = $this->default_update_server['upd_address'] . 'r.php?do=future&platform=xml&ver=' . $this->update_to_version;
				
				// make a data call with this url
				$call_results = KernelBaseSystem::fetchWebFile($get_ver_data);
				
				// valid result?
				if($call_results === false || $call_results === NULL) {
					// update server down?
					return false;
				} else {
					// return update servers's results
					return $call_results;
				}
				
				// end
				break;
			default:
				// unknown command?
				return false;
				break;
		}
	}
	
	/**
	* Add a new item to the info table
	*
	* @param string $text
	* @param string $is_fatal
	* @access public
	*/	
	public function newItem($text, $is_fatal = NULL) {
		switch($is_fatal) {
			case 'ss':
				echo('<tr><td colspan="2" style="border-top: #999 thin solid; color: #FFF; background-color: #3C6B2C;">' . $text . '</td></tr>');
				break;
			case true:
				echo('<tr><td colspan="2" style="border-top: #999 thin solid; color: #FFF; background-color: #b52a1b;">' . $text . '</td></tr>');
				break;
			default:
				echo('<tr><td colspan="2" style="border-top: #999 thin solid; color: #000;">' . $text . '</td></tr>');
				break;
		}
		
		// flush the buffer to echo
		KernelBaseSystem::FlushBuffer();
	}
	
	/**
	* Obtains a file from the update server
	*
	* @param string $text
	* @param return string
	* @access public
	*/	
	public function GetContents($file_location) {
		// call the kernel to get this file
		$temp = KernelBaseSystem::fetchWebFile($this->default_update_server['upd_address'] . $file_location);
		
		// return output
		return $temp;
	}
}
?>